#include <stdio.h>
#include <stdlib.h>
#include<math.h>
struct ListNode
{
    int val;
    struct ListNode*next;
};
//int judgezero(struct ListNode*head)//判断有几个零
//{
//    int count=0;
//    while(head->val==0)
//    {
//        count++;
//        head=head->next;
//    }
//    return count;
//}
//long digit(struct ListNode*head)//取数据
//{
//    if(head->next==NULL)
//    {
//        return head->val;
//    }
//    long tmp =0;
//    struct ListNode*cur=head;
//    while(cur)
//    {
//        tmp=(tmp+cur->val)*10;
//        cur=cur->next;
//    }
//    long count=0;
//    while(tmp)
//    {
//        count=(count+tmp%10)*10;
//        tmp=tmp/10;
//    }
//    if(head->val!=0)
//        return count/10;
//    else if(head->next->val==0)
//    {
//        int ret=judgezero(head->next);
//        int zero=pow(10,ret);
//        return count*zero;
//    }
//    else
//        return count;
//}
////long  digit(struct ListNode*head)//取数据
////{
////    if(head->next==NULL)
////    {
////        return head->val;
////    }
////
////    long int tmp =0;
////    struct ListNode*cur=head;
////    while(cur)
////    {
////        if(cur->next!=NULL)
////            tmp=(tmp+cur->val)*10;
////        else
////            tmp+=cur->val;
////        cur=cur->next;
////    }
////    long  count=0;
////    while(tmp)
////    {
////        if(tmp/10!=0)
////            count=(count+tmp%10)*10;
////        else
////            count +=tmp;
////        tmp=tmp/10;
////    }
////    return count;
////}
//
//
//struct ListNode* apply(int x)//申请节点
//{
//    struct ListNode* newnode=(struct ListNode*)malloc(sizeof(struct ListNode));
//    newnode->val=x;
//    newnode->next=NULL;
//    return newnode;
//}
//void destory(struct ListNode*head)
//{
//    while(head)
//    {
//        struct ListNode*next=head->next;
//        free(head);
//        head=next;
//    }
//}
//struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
//{
//    //1.遍历链表，并把每个节点上的数据拿出来类似2->4->5==245
//    //2.俩链表值相加
//    //3.除模数据尾插新链表
//
//    struct ListNode*newhead=apply(0);//俩链表数据位0
//    if(l1->val==0&&l2->val==0&&l1->next==NULL&&l2->next==NULL)
//    {
//        return newhead;
//
//    }
//    //(1) +  //(2)
//    struct ListNode* n1=l1;
//    struct ListNode* n2=l2;
//    int total =digit(n1);
//    int c=digit(n2);
//
//    //(3)
//    struct ListNode*tail=newhead;
//    while(total)
//    {
//        int stern=total%10;
//        total/=10;
//        struct ListNode* newnode=apply(stern);
//        tail->next=newnode;
//        tail=tail->next;
//    }
//
////销毁链表
//    destory(l1);
//    destory(l2);
//    l1=newhead->next;
//    free(newhead);
//    return l1;
//}


int judgezero(struct ListNode*head)//判断有几个零
{
    int count=0;//应为第一个也是0
    while(head->val==0)
    {
        count++;
        head=head->next;
    }
    return count;
}

long long  digit(struct ListNode*head)//取数据
{
    if(head->next==NULL)
    {
        return head->val;
    }
    long long tmp =0;
    struct ListNode*cur=head;
    while(cur)
    {
        tmp=(tmp+cur->val)*10;
        cur=cur->next;
    }
    long long count=0;
    while(tmp)
    {
        count=(count+tmp%10)*10;
        tmp=tmp/10;
    }
    if(head->val!=0)
        return count/10;
    else if(head->next->val==0)
    {
        int ret=judgezero(head->next);
        long long zero=pow(10,ret);
        return count*zero;
    }
    else
        return count;
}
struct ListNode* apply(int x)//申请节点
{
    struct ListNode* newnode=(struct ListNode*)malloc(sizeof(struct ListNode));
    newnode->val=x;
    newnode->next=NULL;
    return newnode;
}
void destory(struct ListNode*head)
{
    while(head)
    {
        struct ListNode*next=head->next;
        free(head);
        head=next;
    }
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    //1.遍历链表，并把每个节点上的数据拿出来类似2->4->5==245
    //2.俩链表值相加
    //3.除模数据尾插新链表
    struct ListNode*newhead=apply(0);
    if(l1->val==0&&l2->val==0&&l1->next==NULL&&l2->next==NULL)
    {
        return newhead;

    }
    //(1)
    struct ListNode* n1=l1;
    struct ListNode* n2=l2;

    //(2)
    long long total =digit(n1)+digit(n2);

    //(3)
    struct ListNode*tail=newhead;
    while(total)
    {
        long long stern=total%10;
        total/=10;
        struct ListNode* newnode=apply(stern);
        tail->next=newnode;
        tail=tail->next;
    }

//销毁链表
    destory(l1);
    destory(l2);
    l1=newhead->next;
    free(newhead);
    return l1;
}
int main()
{
    struct ListNode*n1= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n2= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n3= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n4= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n5= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n6= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n7= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n8= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*n9= (struct ListNode*)malloc(sizeof(struct ListNode));
    n1->val=1;
    n2->val=0;
    n3->val=0;
    n4->val=1;

    n5->val=5;
    n6->val=4;
    n7->val=6;

    n1->next=n2;
    n2->next=n3;
    n3->next=n4;
    n4->next=NULL;

    n5->next=n6;
    n6->next=n7;
    n7->next=NULL;


    struct ListNode*ret=addTwoNumbers(n1,n6);
}
